//
//  YFNotMatchVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/21.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFNotMatchVC.h"
#import "YFNotMatchDetailVC.h"
#import "YFJourneyModel.h"
#import "HJNotMatch.h"
#import "HJNavigationViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "HJTabBarViewController.h"

/** 未匹配行程状态 **/
#define NOT_MATCH_JOURNEY_STATUS (@"0")

@interface YFNotMatchVC () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

/**
 当前请求的页码
 */
@property (nonatomic, assign) NSInteger page;

/**
 数据源数据
 */
@property (nonatomic, strong) NSArray <YFRecordModel *> *records;

/**
 布局TableView
 */
@property (nonatomic, strong, nonnull) UITableView *tableView;

@property (nonatomic, strong) UIView *empty;

@end

@implementation YFNotMatchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self loadData];
}

- (void)setupViews {
    self.navigaitonBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    self.view.clipsToBounds = NO;
    [self setupTableView];
}

- (void)setupTableView {
    UITableView *tb = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0, self.view.width, self.view.height - Height_TabBar - 10)) style:(UITableViewStylePlain)];
    tb.backgroundColor = [UIColor clearColor]; 
    tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        tb.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    // Height_TabBar + 10 + 16：tabbar高度 + 10的tabbar凸起高度 + 68的按钮高度 + 16的按钮上下间距
    tb.contentInset = UIEdgeInsetsMake(0, 0, Height_TabBar, 0);
//    tb.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, Height_TabBar + 5, 0);
    tb.dataSource = self;
    tb.delegate = self;
    tb.rowHeight = 200;
    [tb registerCellByNibName:@"YFNotMatchCell"];
    
    // 设置空态视图
    tb.emptyView = self.tipEmptyView;
//     上拉刷新，下拉加载
    @weakify(self)
    tb.refresh(^{
        @strongify(self)
        self.page = 1;
        [self loadDatas:self.page];
    })
    .loadMore(^{
        @strongify(self)
        self.page += 1;
        [self loadDatas:self.page];
    });
    
    [self.view addSubview:tb];
    self.tableView = tb;
}

#pragma mark - 加载数据
- (void)loadData {
    self.page = 1;
    self.records = [NSArray array];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _records.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJNotMatch * notMachCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!notMachCell) {
        notMachCell = [[NSBundle mainBundle]loadNibNamed:@"HJNotMatch" owner:nil options:nil].firstObject;
    }
    notMachCell.type = notMatch;
    if (_records && _records.count && indexPath.row < _records.count) {
        [notMachCell loadMatchCellDataSource:self.records[indexPath.row]];
    }
    @weakify(self)
    notMachCell.encounterBlock = ^(NSString *tripId) {
        @strongify(self)
        LKLog(@"未匹配行程id：%@", tripId);
        HJTabBarViewController * tabbar = (HJTabBarViewController *)self.tabBarController;
//        tabbar.playerVC.tripId = tripId;
//        [tabbar.playerVC loadNetworkData];
        [tabbar setSelectedIndex:0];
    };

    return notMachCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YFNotMatchDetailVC *vc = [YFNotMatchDetailVC new];
    vc.journeyModel = _records[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 网络请求
- (void)loadDatas:(NSInteger)page {
    
    NSString *postString = [Api stringByAppendingString:UserJourneyApi];
    NSDictionary *params = @{
                            @"userId": [HJUserManager getUserModel].userId,
                            @"pageCurrent":@(page),
                            @"tripStatus":NOT_MATCH_JOURNEY_STATUS
                            };
    
    @weakify(self)
    [HJHttpTool get:postString params:params success:^(HJResponeds *result) {
        @strongify(self)
        LKLog(@"%@", result);
        
        NSDictionary *dataDict = result.data;
        YFJourneyModel *model = [YFJourneyModel yy_modelWithDictionary:dataDict];
        
        self.records = self.records.addArray(model.records, self.tableView.mj_header.isRefreshing);
        [self.tableView reloadData];
        [self.tableView endRefresh];
        // 到底了
        if (model.pages.integerValue != self.page) { [self.tableView.mj_footer endRefreshing]; }
        self.tableView.mj_footer.hidden = self.records.count == 0;
        
    } failure:^(id result) {
        @strongify(self)
        [self.tableView endRefresh];
        if (self.records.count == 0) {
            self.tipEmptyView.message = result;
            [self.tableView reloadData];
        } else {
            [YFHud showError:result];
        }
    }];
}

@end
